import { Request, Response } from 'express';
import { ILessonDocument, Lesson } from '../models';


export const list = async (req: Request, res: Response) => {
    let { category } = req.query;
    let offset: any = req.query.offset;
    let limit: any = req.query.limit;

    offset = isNaN(offset) ? 0 : parseInt(offset); //偏移量
    limit = isNaN(limit) ? 5 : parseInt(limit);//每页条数

    // 偏移量变更。。0-5 5-5 10-5 15-5 20-5
    let query: Partial<ILessonDocument> = {} as ILessonDocument;
    if (category && category != "all") {
        query.category = category as string;
    }
    let total = await Lesson.count(query);
    let list = await Lesson.find(query).sort({ order: 1 })
        .skip(offset).limit(limit);
    console.log(req.query, '课程请求req.query', total, 'total是多少？', list, 'list多少？');
    setTimeout(() => {
        res.json({
            success: true,
            code: 1,
            data: {
                list,
                hasMore: total > offset + limit
            }
        });
    }, 1000);
}



export const get = async (req: Request, res: Response) => {
    let id = req.params.id;
    console.log(req.params, 'req.params=>多少');
    let lesson = await Lesson.findById(id);
    console.log(lesson,'lesson');
    res.json({ success: true, data: lesson });
  };

